package com.wyu.blog.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@PropertySource("classpath:application.properties")
@MapperScan(basePackages = "com.wyu.blog.dao",sqlSessionTemplateRef = "dbSqlSessionTemplate")
public class DataSourceConfig{
    /**
     * 生成数据源.  @Primary 注解声明为默认数据源
     */
    @Bean(name = "dbDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="mybatis.configuration")
    /**
     * 增加此部分 获取配置文件中的mybatis配置信息
     */
    public org.apache.ibatis.session.Configuration globalConfiguration(){
        return new org.apache.ibatis.session.Configuration();
    }

    /**
     * 创建 SqlSessionFactory
     */
    @Bean(name = "dbSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dbDataSource") DataSource dataSource,
        org.apache.ibatis.session.Configuration config) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        /*加载application 驼峰配置*/
        bean.setConfiguration(config);
        bean.setTypeAliasesPackage("com.wyu.blog.entity");
        return bean.getObject();
    }

    /**
     * 配置事务管理
     */
    @Bean(name = "dbTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("dbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "dbSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("dbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    //配置Druid的监控
    //1、配置一个管理后台的Servlet
//    @Bean
//    @Primary
//    public ServletRegistrationBean statViewServlet(){
//        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/mapper/*");
//        Map<String,String> initParams = new HashMap<>();
//
//        initParams.put("loginUsername","admin");
//        initParams.put("loginPassword","123456");
//        initParams.put("allow","");//默认就是允许所有访问
//        //initParams.put("deny","192.168.15.21");
//
//        bean.setInitParameters(initParams);
//        return bean;
//    }
//
    //2、配置一个web监控的filter
//    @Bean
//    @Primary
//    public FilterRegistrationBean webStatFilter(){
//        FilterRegistrationBean bean = new FilterRegistrationBean();
//        bean.setFilter(new WebdavFixFilter());
//
//        Map<String,String> initParams = new HashMap<>();
//        initParams.put("exclusions","*.js,*.css,/druid/*");
//
//        bean.setInitParameters(initParams);
//
//        bean.setUrlPatterns(Arrays.asList("/*"));
//
//        return  bean;
//    }
}
